import Vue from "vue";
import VueRouter from "vue-router";
Vue.use(VueRouter);

const routes = [
  // {
  //   path: "/",
  //   name: "Index",
  //   component: () => import("../home.vue"),
  //   meta: { title: "首页" },
  // },
  {
    path: '/',
    redirect: '/activity' // 重定向
  },
  {
    path: "/guidepage",
    name: "guidepage",
    component: () => import("../components/guidepage/guidepage.vue"),
    meta: { title: "首页" },
  },
  {
    path: "/guidancepage",
    name: "guidancepage",
    component: () => import("../components/guidancepage/guidancepage.vue"),
    meta: { title: "指引页" },
  },
  {
    path: "/login",
    name: "login",
    component: () => import("../login.vue"),
    meta: { title: "登录" },
  },
  {
    path: "/activity",
    name: "activity",
    component: () => import("../components/activityInfo.vue"),
  },
  {
    path: "/message",
    name: "message",
    component: () => import("../components/messagename.vue"),
  },
  {//分享成功
    path: "/successfullyshared",
    name: "successfullyshared",
    component: () => import("../components/components/successfullyshared/successfullyshared.vue"),
  },
  {//首页banner 分享/筛查
    path: "/bannersearch",
    name: "bannersearch",
    component: () => import("../components/components/bannersearch/bannersearch.vue"),
  },
  {
    path: "/yanhua",
    name: "yanhua",
    component: () => import("../components/yanhua/yanhua.vue")
  },
  {//完成第一组筛查  screening?id=1&path=mmse  完成第一组筛查  screening?id=2&path=emt 完成第二组筛查
    path: "/screening",
    name: "Screening",
    component: () => import("../components/components/Screening/Screening.vue"),
  },
  { //完成信息填写
    path: "/onebasicinformation",
    name: "onebasicinformation",
    component: () => import("../components/components/onebasicinformation/onebasicinformation.vue"),
  },
  { //谢谢参与
    path: "/completeactivity",
    name: "completeactivity",
    component: () => import("../components/components/completeactivity/completeactivity.vue"),
  },
  { //收钱了 - 加入训练营 collectmoney?id=1  和完成信息填写collectmoney?id=2  和 完成筛查collectmoney?id=1   体现卡使用 
    path: "/collectmoney",
    name: "collectmoney",
    component: () => import("../components/components/collectmoney/collectmoney.vue"),
  },
  { //加入训练营
    path: "/Jointrainingcamp",
    name: "Jointrainingcamp",
    component: () => import("../components/components/Jointrainingcamp/Jointrainingcamp.vue"),
  },
  { //提现失败
    path: "/withdrawalfailed",
    name: "withdrawalfailed",
    component: () => import("../components/components/withdrawalfailed/withdrawalfailed.vue"),
  },
  // { //筛查完成弹窗
  //   path: "/completescreening",
  //   name: "completescreening",
  //   component: () => import("../components/components/completescreening/completescreening.vue"),
  // },
  { //滚动金额页面  messageBox?id=2基础信息填写   messageBox?id=1 完成第一组筛查
    path: "/messageBox",
    name: "MessageBox",
    component: () => import("../components/components/MessageBox/MessageBox.vue"),
  },
  { //金额提醒  查询红包接口 basicinformationcompleted?id=2 基础信息填写    basicinformationcompleted?id=1 完成第一组筛查
    path: "/basicinformationcompleted",
    name: "basicinformationcompleted",
    component: () => import("../components/components/basicinformationcompleted/basicinformationcompleted.vue"),
  },
  {
    path: "/messagecity",
    name: "messagecity",
    component: () => import("../components/messagecity.vue"),
  },
  {
    path: "/result",
    name: "result",
    component: () => import("../components/pages/middleScreening/result/result.vue"),
  },
  {
    path: "/mine",
    name: "mine",
    component: () => import("../components/mine.vue"),
  },
  {
    path: "/detail",
    name: "detail",
    component: () => import("../components/pages/middleScreening/detail.vue"),
  },
  {
    path: "/indexButtonone",
    name: "indexButtonone",
    component: () => import("../components/pages/index/publicArticle/indexButtonone.vue"),
  },
  {
    path: "/share",
    name: "share",
    component: () => import("../components/share.vue"),
  },
  {
    path: "/sce-end",
    name: "SCE-END",
    component: () => import("../components/SCE/end.vue"),
    meta: {
      title: "【爱脑行动】认知风险筛查",
    },
  },
  {
    path: "/mmse",
    name: "MMSE",
    component: () => import("../components/MMSE/index.vue"),
    meta: {
      title: "【爱脑行动】认知风险筛查",
    },
  },
  {
    path: "/mmse-main",
    name: "MMSE-MAIN",
    component: () => import("../components/MMSE/main.vue"),
    meta: {
      title: "【爱脑行动】认知风险筛查",
    },
  },
  {
    path: "/mmse-end",
    name: "MMSE-END",
    component: () => import("../components/MMSE/end.vue"),
    meta: {
      title: "【爱脑行动】认知风险筛查",
    },
  },
  {
    path: "/emt",
    name: "EMT",
    component: () => import("../components/EMT/index.vue"),
    meta: {
      title: "【爱脑行动】认知风险筛查",
    },
  },
  {
    path: "/emt-main",
    name: "EMT-MAIN",
    component: () => import("../components/EMT/main.vue"),
    meta: {
      title: "【爱脑行动】认知风险筛查",
    },
  },
  {
    path: "/emt-end",
    name: "EMT-END",
    component: () => import("../components/EMT/end.vue"),
    meta: {
      title: "【爱脑行动】认知风险筛查",
    },
  },
  {
    path: "/sce",
    name: "SCE",
    component: () => import("../components/SCE/index.vue"),
    meta: {
      title: "【爱脑行动】认知风险筛查",
    },
  },
  {
    path: "/sce-main",
    name: "SCE-MAIN",
    component: () => import("../components/SCE/main.vue"),
    meta: {
      title: "【爱脑行动】认知风险筛查",
    },
  },
  {
    path: "/sce-end",
    name: "SCE-END",
    component: () => import("../components/SCE/end.vue"),
    meta: {
      title: "【爱脑行动】认知风险筛查",
    },
  },
  {
    path: "/mini-report",
    name: "miniReport",
    component: () => import("../report/mini-report.vue"),
    meta: {
      title: "测评结果",
    },
  },
  // {
  //   path: "/index",
  //   name: "shouye",
  //   component: () => import("../components/pages/index/index.vue"),
  //   children: [
  //     {
  //       path: '/index/detail',
  //       name: 'detail',
  //       component: () => import('../components/pages/middleScreening/detail.vue'),
  //     },
  //   ]
  // },

];

const router = new VueRouter({
  routes,
  mode: "hash",
});

// 为路由对象 挂载路由导航守卫
router.beforeEach((to, from, next) => {
  // to 将要访问的的路径
  // from 代表从哪个路径跳转
  // next 是一个函数 表示放行 next() 放行 next('/login') 强制跳转
  //如果用户访问的登录页面，直接放行
  // 获取存储token的开始时间
  const tokenStartTime = localStorage.getItem('tokenStartTime');
  if (tokenStartTime != undefined || tokenStartTime != null) {
    // 后台给出的token有效时间，一个星期 单位 是秒
    // 我们自己定义6天过期，让用户重新登录一下， 用户总不可能在一个页面挂机一天吧
    const timeOver = 6 * 24 * 3600 * 1000;
    // 当前时间
    let date = new Date().getTime();
    // 如果大于说明是token过期了
    if (date - tokenStartTime > timeOver) {
      localStorage.removeItem("token");
      next()
    }
  }
  if (to.path === '/login') return next()
  if (to.path === '/guidancepage') return next()
  if (to.path === '/guidepage') return next()
  // if (to.path === '/onebasicinformation') return next()
  // if (to.path === '/screening') return next()
  // 从sessionStorage中获取到保存的 token值
  const tokenStr = localStorage.getItem('token')
  //没有token 强制跳转到登录页
  if (!tokenStr) {
    return next('/guidepage')
  }
  next()
})

const originalPush = VueRouter.prototype.push;
const originalReplace = VueRouter.prototype.replace;
//push
VueRouter.prototype.push = function push(location, onResolve, onReject) {
  if (onResolve || onReject)
    return originalPush.call(this, location, onResolve, onReject);
  return originalPush.call(this, location).catch(err => err);
};
//replace
VueRouter.prototype.replace = function push(location, onResolve, onReject) {
    if (onResolve || onReject)
        return originalReplace.call(this, location, onResolve, onReject);
    return originalReplace.call(this, location).catch(err => err);
};

export default router;
